﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Web;
using System.Web.Services;
using Netcare.Web.Utils;

namespace Netcare.Web.Services
{
    /// <summary>
    /// Summary description for main
    /// </summary>
    [WebService(Namespace = "http://www.wicresoft.com/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]

    public class Main : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "一串很长的中文字！" + Environment.NewLine
                + "Cal Lightman博士是位权威的鉴谎专家，他天赋异禀，能轻易看穿精心打造的各种谎言，愤怒、愧疚、蔑视、悲伤，一切微表情都逃不过他的眼睛。他的搭档Gillian Foster博士，精通心理学，极富洞察力，相较于Lightman的直截了当，更善处理人际关系。Eli Loker是Lightman的研究员，他最初坚持只说真话，后来却慢慢破了戒，最终被降为实习生，停薪查看。Ria Torres是Lightman从机场挖来的鉴谎天才，她初出茅庐却野心勃勃，妄图挑战Lightman的权威。";
        }

        [WebMethod]
        public ProcessInfo2[] QueryProcesses()
        {
            List<ProcessInfo2> list = new List<ProcessInfo2>();

            foreach (var p in Process.GetProcesses())
            {
                try
                {
                    list.Add(new ProcessInfo2()
                    {
                        PID = p.Id,
                        ImageName = p.MainModule.ModuleName,
                    });
                }
                catch { }
            }

            return list.ToArray();
        }

        [WebMethod]
        public void KillProcess(int PID)
        {
            Process.GetProcessById(PID).Kill();
        }

        [WebMethod]
        public AgentInfo GetAgentInfo()
        {
            return new AgentInfo()
            {
                Name = Assembly.GetExecutingAssembly().FullName,
                AvailableServices = ReflectionUtil.GetSpecificTypes(
                    Assembly.GetExecutingAssembly(), typeof(WebServiceAttribute)),
            };
        }


        [WebMethod]
        public Version GetVersion()
        {
            return Assembly.GetExecutingAssembly().GetName().Version;
        }

    }

    [Serializable]
    public class ProcessInfo2
    {
        public int PID { get; set; }
        public string ImageName { get; set; }
        public string UserName { get; set; }
        public string Description { get; set; }
    }

    [Serializable]
    public class AgentInfo
    {
        public string Name { get; set; }
        public Version Version { get; set; }
        public string[] AvailableServices { get; set; }
    }
}
